﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Data;
using System.Configuration;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Profile;


/// <summary>
/// Summary description for DBClass
/// </summary>
public class DBClass
{
    private static DBClass defaultInsatance = new DBClass();

    public static DBClass Default
    {
        get { return defaultInsatance; }
    }

    string connectionStr;

    public DBClass()
    {
        connectionStr = ConfigurationManager.ConnectionStrings["conn"].ToString();
        // connectionStr = @"Data Source=.\SQLEXPRESS;database=TEST;trusted_connection=yes";
        // connectionStr = @"Data Source=.\S-PC;database=taxi;username=tccuser;password=tuser";
    }

    public bool InsertCommand(string queryStr)
    {
        SqlConnection cnn = new SqlConnection(connectionStr);
        try
        {
            cnn.Open();
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Bağlantı sağlanamadı.", "Uyarı", MessageBoxButtons.OK);
            return false;
        }
        try
        {
            SqlCommand insert = new SqlCommand(queryStr, cnn);
            insert.ExecuteNonQuery();
            cnn.Close();
        }
        catch (Exception ex)
        {

            return false;
        }
        return true;
    }

    public bool UpdateCommand(String updateCommand)
    {
        SqlConnection cnn = new SqlConnection(connectionStr);
        try
        {
            cnn.Open();
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Bağlantı sağlanamadı.", "Uyarı", MessageBoxButtons.OK);
            return false;
        }
        try
        {
            SqlCommand update = new SqlCommand(updateCommand, cnn);
            update.ExecuteNonQuery();
            cnn.Close();
        }
        catch (Exception ex)
        {

            return false;
        }
        return true;
    }

    public DataSet SelectCommand(string queryStr)
    {
        SqlConnection cnn = new SqlConnection(connectionStr);
        try
        {
            cnn.Open();
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Bağlantı sağlanamadı.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
            cnn.Close();
            return null;
        }

        try
        {
            DataSet tempSet = new DataSet();
            SqlDataAdapter selectAdapter = new SqlDataAdapter(queryStr, cnn);
            selectAdapter.Fill(tempSet);
            return tempSet;
        }
        catch (Exception ex)
        {

            cnn.Close();
            return null;
        }
        finally
        {
            cnn.Close();
        }
    }

    //public DataSet sezEry(string queryStr)
    //{
    //    SqlCommand cmd = new SqlCommand(queryStr);
    //    SqlDataAdapter da = new SqlDataAdapter();
    //    DataSet ds = new DataSet();
    //    //DataTable dt = new DataTable();
    //    SqlConnection cnn = new SqlConnection(connectionStr);
    //    cmd.Connection = cnn;
    //    da.SelectCommand = cmd;
    //    //da.Fill(dt);
    //    da.Fill(ds, "User");
    //    cnn.Close();
    //    return ds;
    //}

    public bool DeleteCommand(string queryStr)
    {
        SqlConnection cnn = new SqlConnection(connectionStr);
        try
        {
            cnn.Open();
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Bağlantı sağlanamadı.", "Uyarı", MessageBoxButtons.OK);
            return false;
        }
        try
        {
            SqlCommand insert = new SqlCommand(queryStr, cnn);
            insert.ExecuteNonQuery();
            cnn.Close();
        }
        catch (Exception ex)
        {

            cnn.Close();
            return false;
        }
        return true;
    }

    public bool ExecuteCommand(string queryStr)
    {
        SqlConnection cnn = new SqlConnection(connectionStr);
        try
        {
            cnn.Open();
        }
        catch (Exception ex)
        {

            //MessageBox.Show("Bağlantı sağlanamadı.", "Uyarı", MessageBoxButtons.OK);
            if (cnn.State == ConnectionState.Open) cnn.Close();
            return false;
        }
        try
        {
            SqlCommand command = new SqlCommand(queryStr, cnn);
            command.ExecuteNonQuery();
            cnn.Close();
        }
        catch (Exception ex)
        {

            return false;
        }
        return true;
    }
    public DataSet GetAllUsers()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt = ds.Tables.Add("Users");

        MembershipUserCollection muc;
        muc = Membership.GetAllUsers();
        dt.Columns.Add("UserName", Type.GetType("System.String"));
        dt.Columns.Add("Email", Type.GetType("System.String"));
        dt.Columns.Add("CreationDate", Type.GetType("System.DateTime"));
      
        foreach (MembershipUser mu in muc)
        {
            DataRow dr;
            dr = dt.NewRow();
            dr["UserName"] = mu.UserName;
            dr["Email"] = mu.Email;
            dr["CreationDate"] = mu.CreationDate;

            dt.Rows.Add(dr);
        }
        return ds;
    }
    public bool DeleteUser(string username)
    {
        return Membership.DeleteUser(username);
    }
	 public bool addLog(string userName, string LogType, string Details, DateTime logDate)
    {
        bool res;
        string qry = "INSERT INTO dbo.Logs (UserName, Type, Details, Date) VALUES ('" + userName + "','" + LogType + "','" + Details + "','" + logDate.ToString("s") + "')";
        res = DBClass.Default.InsertCommand(qry);
        return res;
    }
}